import time
import pytest
from appium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from config import config
from base import Base


class TestApp:

    def setup(self):
        print('setUp')
        # appium服务监听地址
        server = 'http://localhost:4723/wd/hub'
        # app启动参数
        desired_caps = {
            "platformName": "Android",
            "deviceName": "OnePlus",
            "appPackage": "com.mapsoft.bustrip_tianjin",
            "appActivity": "com.mapsoft.bustrip_tianjin.MainActivity",
            "noReset": "False"
        }
        aid = desired_caps['appPackage'] + ':id/'
        # 驱动
        driver = webdriver.Remote(server, desired_caps)
        wait = WebDriverWait(driver, 30)

        self.driver = driver
        self.wait = wait
        # 分割线----------------------------------
        b = Base(self.driver)
        time.sleep(6)

        if self.is_element_exist('首页'):
            print('首页')

        else:
            print('其他界面')
            for i in range(2):
                b.swipe_left()
                time.sleep(1)
            # 立即进入
            driver.tap([(536, 1696)], 200)
            # click(536, 1696)

        # 同意隐私
        self.click_id('agree_agreement')
        time.sleep(3)
        # 查看字体
        print('查看 driver')
        print(driver)
        location = driver.find_element(By.XPATH, '//android.view.View[@text="查看"]/..')
        print('location')
        print(location)
        location.click()
        print('点击查看')
        time.sleep(3)
        me = driver.find_element(By.XPATH, '//android.widget.TextView[@text="我的"]/..')
        print('我的')
        me.click()
        time.sleep(3)
        login = driver.find_element(By.XPATH, '//android.view.View[@text="我要登录"]')
        print('登录')
        login.click()
        time.sleep(3)

        # .activity.login.LoginWebViewActivity2

    def teardown(self):
        print('tearDown')
        self.driver.quit()
        pass

    def is_element_exist(self, element):
        source = self.driver.page_source
        print(source)
        if element in source:
            return True
        else:
            return False

    def click_id(self, ids):
        self.driver.find_element(By.ID, 'com.mapsoft.bustrip_tianjin:id/' + ids).click()

    @pytest.mark.parametrize('data', config)
    def test_case1(self, data):
        print('t1')
        print("预期结果", data['er'])
        assert self.is_element_exist('请输入手机号')
        # 立即开启
        phone = self.driver.find_element(By.XPATH, '//android.widget.EditText[@text="请输入手机号"]')
        phone.send_keys(data['user'])
        time.sleep(1)

        pasw = self.driver.find_element(By.XPATH, '//android.widget.EditText[@text="请输入密码"]')
        pasw.send_keys(data['pass'])
        time.sleep(1)

        yuedu = self.driver.find_element(By.XPATH, '//android.view.View[@text="我已阅读并同意"]/preceding-sibling::android.widget.Image[3]')
        print('同意协议')
        yuedu.click()
        time.sleep(1)
        login_button = self.driver.find_elements(By.XPATH, '//android.view.View[@text="登录"]/..')
        print('点击登录')
        login_button[1].click()
        print('登录click')
        time.sleep(3)
        assert self.is_element_exist(data['er'])
        time.sleep(2)


if __name__ == '__main__':
    pytest.main(['-s', './main.py', '--html=report.html'])
